<?php
namespace app\common\logic;
use think\Model;
use think\facade\Db;
/**
 * 自定义页面处理
 * Class CatsLogic
 * @package Home\Logic
 */
class PageLogic
{
    public function __construct($data=[])
    {

    }

    public function initpagecontent($pagecontent,$user_id=0){

        $pagecontent = json_decode($pagecontent,true);

        if($user_id > 0){
            $member = Db::name('users')->where('user_id',$user_id)->find();
        }else{
            $member = [];
        }
//        htmlspecialchars_decode()
        foreach($pagecontent as $k=>$v){

            if($v['temp'] == 'text'){ //文本
                $showcontent = $v['params']['content'];

                $pagecontent[$k]['params']['showcontent'] = $showcontent;
            }
            if($v['temp'] == 'product'){//产品列表 获取产品信息
                $result = $this->getGoodsPage($v);
                $pagecontent[$k]['data'] = $result;
            }elseif($v['temp'] == 'search'){
                $pagecontent[$k]['params'] = $v['params'];
            }elseif($v['temp'] == 'menu'){
                $data = $v['data'];
                $newdata = [];
                if(!$v['params']['pernum']){
                    $v['params']['pernum'] = 10;
                    $pagecontent[$k]['params']['pernum'] = 10;
                }
                $pagecount = ceil(count($data)/$v['params']['pernum']);
                if(!$pagecount) $pagecount = 1;
                for($i=0;$i<$pagecount;$i++){
                    $newdata[$i]=array_slice($data,$v['params']['pernum']*$i,$v['params']['pernum']);
                }
                if($v['params']['showicon']==0){
                    $pagecontent[$k]['params']['iconsize'] = 0;
                }
                //dump($newdata);
                $pagecontent[$k]['params']['newdata'] = $newdata;
                $pagecontent[$k]['params']['newdata_linenum'] = ceil($v['params']['pernum']/$v['params']['num']);
            }elseif($v['temp']=='userinfo'){
                $rdata = $this->getUserPage($v,$user_id);
                $pagecontent[$k]['data'] = $rdata;
            }elseif($v['temp'] == 'seckill'){//产品列表 获取产品信息
                $result = $this->getSeckillPage($v);
                $pagecontent[$k]['data'] = $result;
            }
        }

        return json_encode($pagecontent);
    }

    //获取普通产品页面内容
    public function getGoodsPage($content){
        $proshownum = $content['params']['proshownum'];
        $map = [];
        if(!empty($content['params']['leixing_id'])){
            $map['leixing_id'] = $content['params']['leixing_id'];
        }
        $map['is_on_sale'] = 1;
        $map['status'] = 1;
        $field = 'goods_id,cat_id,goods_name,sid,click_count,brand_id,store_count,comment_count,weight,market_price,shop_price,goods_remark,original_img,is_on_sale
                ,sales_sum,leixing_id,cat_id_1,cat_id_2,cat_id_3,pics,status';
        //$result = Db::name('goods')->where($map)->limit($proshownum)->field($field)->order('sort desc,goods_id desc')->select();
        $listdata= ['map'=>$map,'order'=>'sort desc,goods_id desc','field'=>$field];
        $result = logic('GoodsLogic')->getListNew($listdata,true);
//                $result = [];
        if($result){
            foreach($result as $pk=>$pv){
                if(strpos($pv['original_img'],'http')===false){
                    $result[$pk]['original_img'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$pv['original_img'];
                }
            }
        }
        return $result;
    }
    //获取秒杀产品页面内容
    public function getSeckillPage($content){
        $proshownum = $content['params']['proshownum'];
        $map = [];
        $map[] = ['status','=',1];
        if(input('bid')){
            $map[] = ['sid','=',input('bid')];
        }
        $goods_ids = Db::name('seckill_product')
            ->where('status',1)
            ->where('stime','<=',time())->where('etime','>=',time())->column('goods_id');
        $map = [];
        $map[] = ['is_on_sale','=',1];
        $map[] = ['status','=',1];
        $map[] = ['goods_id','in',$goods_ids];
        if(input('leixing_id')){
            $map['leixing_id'] = input('leixing_id');
        }
        if(input('bid')){
            $map['sid'] = input('bid');
        }
        $field = 'goods_id,cat_id,goods_name,sid,click_count,brand_id,store_count,comment_count,weight,market_price,shop_price,goods_remark,original_img,is_on_sale
                ,sales_sum,leixing_id,cat_id_1,cat_id_2,cat_id_3,pics,status';
        $result = Db::name('goods')->where($map)->field($field)->limit($proshownum)->select()->toArray();
        logic('GoodsLogic')->getPriceNew($result,0);
        //                $result = [];
        if($result){
            foreach($result as $pk=>$pv){
                if(strpos($pv['original_img'],'http')===false){
                    $result[$pk]['original_img'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].$pv['original_img'];
                }
                $result[$pk]['seckill_status'] = 1;
                $nowtime = time();
                $seckill_endtime = $pv['etime'];
                $lefttime = $seckill_endtime - $nowtime;
                $result[$pk]['hour'] = floor($lefttime / 3600);
                $result[$pk]['minute'] = floor(($lefttime - $result[$pk]['hour'] * 3600) / 60);
                $result[$pk]['second'] = $lefttime - ($result[$pk]['hour'] * 3600) - ($result[$pk]['minute'] * 60);//带天数
                $result[$pk]['day'] = floor($lefttime / 86400);
                $result[$pk]['day_hour'] = floor(($lefttime - $result[$pk]['day'] * 86400) / 3600);
            }
        }
        return $result;
    }

    //获取会员信息页内容
    public function getUserPage($content,$user_id){
        $rdata = [];
        $userinfo = Db::name('users')->where('user_id',$user_id)->find();
        if(!$userinfo){
            $userinfo = [
                'user_id' => 0,
                'nickname' => '游客',
                'headimg' => getCurUrl().'/static/img/touxiang.png',
                'level' => 1,
                'tjr_id' => 0
            ];
        }
        $userinfo['nickname'] = $userinfo['nickname']?:$userinfo['username'];
        $userlevel = Db::name('user_level')->where('rank',$userinfo['level'])->find();
        $orderinfo = [
            'count0' => Db::name('order')->where(['user_id'=>$user_id,'order_status'=>0])->count(),
            'count1' => Db::name('order')->where(['user_id'=>$user_id,'order_status'=>1])->count(),
            'count2' => Db::name('order')->where(['user_id'=>$user_id,'order_status'=>2])->count(),
            'count3' => Db::name('order')->where(['user_id'=>$user_id,'order_status'=>3])->count(),
        ];
        $wallet = Db::name('wallet')->where('user_id',$userinfo['user_id'])->find();
        if(!$wallet){
            $wallet = [
                'money' => 0,
                'score' => 0,
                'gouwuquan' => 0,
                'shouyi' => 0
            ];
        }
        $parent = Db::name('users')->where('user_id',$userinfo['tjr_id'])->find();
        $rdata['userinfo'] = $userinfo;
        $rdata['wallet'] = $wallet;
        $wallet_arr = logic('Wallet')->getName();
        $params = $content['params'];
        $new_wallet_arr = [];
        foreach($wallet_arr as $w_k=>$w_name){
            $new_wallet = [];
            $new_wallet['wallet_key'] = $w_k;
            $new_wallet['wallet_name'] = $w_name;
            $new_wallet['is_show'] = $params[$w_k.'show']??0;
            $new_wallet['value'] = $wallet[$w_k]??0;
            $new_wallet_arr[] = $new_wallet;
        }
        $rdata['wallet_arr'] = $new_wallet_arr;
        $rdata['userlevel'] = $userlevel;
        $rdata['orderinfo'] = $orderinfo;
        $rdata['parent'] = $parent;
        $rdata['parent_show'] = 1;
        return $rdata;
    }
}